"===============================================================================
" vimrc - VIM configure files.
" Last Modified: <2011-11-04 11:18:49 by Jeffy Du>
"
"  Author: Jeffy Du (cmdxiaoha@163.com)
" Version: 1.0
" Created: 2011-10-23 13:44:22
"
" This configure file can use to "win32", "unix", "gui", "console"
" environments.
"===============================================================================

" GENERAL SETTINGS: {{{1
" To use VIM settings, out of VI compatible mode.
set nocompatible

" Edit settings
syntax on " syntax highlighting
filetype plugin indent on " enable file type detection
set autoindent " auto indent
set smartindent " smart indent
set cindent " use C style indent
set cinoptions=:0 " C style indent options
set nobackup " don't backup file
set backupcopy=yes " overwrite the original file
set autoread " auto read when file is modified outsite
set autowrite " auto write when switch to other buffer
set hidden " you can switch to other buffer when buffer is modified
set expandtab " use spaces to instead <Tab>
set tabstop=4 " number of spaces to <Tab>
set softtabstop=4 " number of spaces to delete <Tab>
set shiftwidth=4 " number of spaces when use "<<" and ">>" commands
set textwidth=80 " max chars in one line
set backspace=indent,eol,start " <Backspace> can delete <Ret> char
set whichwrap=b,s,<,>,h,l,[,],~ " these command wrap to next/prev line
set foldenable " use fold
set foldmethod=marker " fold text accoding to marker

" Bell settings
set noerrorbells " no error bell
set novisualbell " no visual bell
set t_vb= " no terminal bell

" Search settings
set hlsearch " highlight match when search
set incsearch " incremental search
set ignorecase " ignore U/L case
set smartcase " don't ignore case when search begin with Upper case

" Coding settings
set encoding=utf-8
set termencoding=utf-8
set fileencoding=gb2312
set fileencodings=gb2312,utf-8,gbk,gb18030,chinese
set fileformat=unix

" Window settings
set number " show line number
set cursorline " highlight current line
set ruler " show line and column number on status line
set showcmd " show typed command on command line
set laststatus=2 " always show status line
set mouse=a " use mouse in terminal mode
set scrolloff=5 " mouse point always beyond from top and button
set nowrap " don't wrap a long line to multi-lines
set completeopt=longest,menuone " complete options
set pumheight=10 " complete popup height

" Color settings
if !has("gui_running")
    set background=dark " easy to read
    colorscheme evening " this color scheme is good both in GUI and terminal
    hi Pmenu        ctermbg=yellow  ctermfg=black
    hi PmenuSel     ctermbg=green   ctermfg=grey
    hi Folded       ctermbg=none    ctermfg=darkgrey
    hi CursorLine   cterm=underline
endif

" Misc settings
set history=100
set helpheight=10 " height of help window
set helplang=cn " use chinese help language if exists
set wildignore=*.bak,*.o,*.e,*~ " ignore file intentions when grep
set wildmenu " command line used
set wildmode=list:longest,full " command line used

" GUI settings
if has("gui_running")
    set guioptions-=T " no toolbar
    set guioptions-=r " no right-hand scrollbar
    set guioptions-=R " no right-hand vertically scrollbar
    set guioptions-=l " no left-hand scrollbar
    set guioptions-=L " no left-hand vertically scrollbar
    autocmd GUIEnter * winsize 100 30 " window width and height
    source $VIMRUNTIME/delmenu.vim " the original menubar has an error on win32, so
    source $VIMRUNTIME/menu.vim    " use this menubar
    language messages zh_CN.utf-8 " use chinese messages if has
endif

" Restore the last quit position when open file.
autocmd BufReadPost *
    \ if line("'\"") > 0 && line("'\"") <= line("$") |
    \     exe "normal g'\"" |
    \ endif

" KEY BINDINGS: {{{1
" Set mapleader
let mapleader=","

" Quick run command
nmap <Space> :
vmap <Space> :

" Quick switch to other window
nnoremap <C-h> <C-w>h
nnoremap <C-j> <C-w>j
nnoremap <C-k> <C-w>k
nnoremap <C-l> <C-w>l

" Switch to prev buffer
nmap <Leader><Leader> <C-^>

" PLUGIN SETTINGS: {{{1
" setting default variables {{{2
if has("win32") " win32 system
    let $VIMFILES = $VIM . "/vimfiles"
    let $HOME  = $VIM
else " unix
    let $VIMFILES = $HOME . "/.vim"
    let $HOME  = $HOME
endif

let g:Author  = "Jeffy Du"
let g:Email   = "cmdxiaoha@163.com"
let g:Company = "SIC Microelectronics CO. Ltd"

" autodata.vim - Auto update last change item {{{2
" Write "Last Modified: <>" to head of file, then date will be update automatic
" when save file.
let g:autodate_keyword_pre = 'Last Modified: <'
let g:autodate_keyword_post  = '>'
let g:autodate_format = "%Y-%m-%d %H:%M:%S by " . g:Author

" auto-pairs.vim - Auto complete paris {{{2
let g:AutoPairsShortcuts = 0
let g:AutoPairsMapBS = 0
let g:AutoPairsMapCR = 0
let g:AutoPairsShortcutToggle = '<C-M-p>'
let g:AutoPairsShortcutFastWrap = '<C-M-e>'
inoremap <buffer> <silent> <M-j> <Esc>:call AutoPairsJump()<CR>

" bufexplorer.vim - Buffer explorer {{{2
" use ":help bufexplorer"

" cmdline-complete.vim - Complete in command line {{{2
" use "<C-n>" and "<C-p>" to complete word in buffer

" GetScripts.vim - get the newest scripts version. {{{2
" run :Getscripts command.

" gtags.vim - use global for browse code {{{2
let g:Gtags_OpenQuickfixWindow = 0
nmap <Leader>gc :!gtags -v<cr>
nmap <Leader>gg :Gtags -g
nmap <Leader>gt :Gtags -t 
nmap <Leader>gr :Gtags -r 
nmap <Leader>gs :Gtags -s 
nmap <Leader>gf :Gtags -f %<cr>
nmap <Leader>gp :Gtags -P 
nmap <silent><C-\> :GtagsCursor<cr>:cclose<cr>
nmap <C-n> :cn<cr>
nmap <C-p> :cp<cr>
autocmd BufWritePost *.h,*.c call system('global -u')

" lookupfile.vim - Search files in project {{{2
" <f5> - open and close lookupfile window.
" This plugin is conflict with neocomplcache.vim, so i changed something.
" You can find the changed command in "install.sh" file.
let g:LookupFile_TagExpr = '"filetags"'
let g:LookupFile_MinPatLength = 2
let g:LookupFile_PreserveLastPattern = 0
let g:LookupFile_PreservePatternHistory = 1
let g:LookupFile_AlwaysAcceptFirst = 1
let g:LookupFile_AllowNewFiles = 0

" mru.vim - Most recent file {{{2
" <F2> - open MRU window
" q - close MRU window
let MRU_File = $HOME . "/.vim_mru"
let MRU_Window_Height = 10
nmap <F2> :MRU<cr>

" neocomplcache.vim - Auto complete {{{2
let g:neocomplcache_enable_at_startup = 1
let g:neocomplcache_enable_camel_case_completion = 1
let g:neocomplcache_enable_underbar_completion = 1
let g:neocomplcache_min_syntax_length = 3
let g:neocomplcache_lock_buffer_name_pattern = '\*ku\*'
let g:neocomplcache_snippets_dir = $VIMFILES . "/snippets"
let g:neocomplcache_temporary_dir = $HOME . "/.vim_neocon"
inoremap <expr><S-Tab> neocomplcache#complete_common_string()
imap     <expr><TAB> pumvisible() ? "\<C-n>" : neocomplcache#sources#snippets_complete#expandable() ? "\<Plug>(neocomplcache_snippets_expand)" : "\<TAB>"
smap     <expr><TAB> pumvisible() ? "\<C-n>" : neocomplcache#sources#snippets_complete#expandable() ? "\<Plug>(neocomplcache_snippets_expand)" : "\<TAB>"
inoremap <expr><CR>  pumvisible() ? neocomplcache#smart_close_popup() : AutoPairsReturn()
inoremap <expr><BS>  neocomplcache#smart_close_popup().AutoPairsDelete()
inoremap <expr><C-h> neocomplcache#smart_close_popup().AutoPairsDelete()
inoremap <expr><C-y> neocomplcache#close_popup()
inoremap <expr><C-e> neocomplcache#cancel_popup()

" NERD_commenter.vim - Commenter & uncommenter {{{2
" use ":help NERD_commenter.txt" to view help.

" NERD_tree.vim - File tree explorer {{{2
" <F3> - open and close NERDTree window
let g:NERDTreeWinPos = "left"
let g:NERDTreeWinSize = 34
let g:NERDTreeShowLineNumbers = 1
let g:NERDTreeQuitOnOpen = 1
nmap <F3> :NERDTreeToggle<cr>

" pdm.vim - Project database management {{{2
" ,dc - create pdm, then input the <name> <ctagsflag> <cscopeflag>
"       example, ":PDMCreate myapp 1 1".
" ,dl - load pdm
" ,du - update pdm
" ,dr - cd to root path of this project
" ,df - cd to file path of current file
"
" After load pdm, you can use lookupfile,omnicppcomplete plugins,
" the tags file is update automatically after you save file, 
" but the cscope files must be update manually.
" This plugin is my own, welcome to use it. :)
let g:PDM_Dir = $HOME . "/.vim_pdm"

" taglist.vim - List tags in file {{{2
" <F4> - open and close taglist window
let g:Tlist_WinWidth = 25
let g:Tlist_Use_Right_Window = 1
let g:Tlist_Auto_Update = 1
let g:Tlist_Process_File_Always = 1
let g:Tlist_Exit_OnlyWindow = 1
let g:Tlist_Show_One_File = 1
let g:Tlist_Enable_Fold_Column = 0
let g:Tlist_Auto_Highlight_Tag = 1
let g:Tlist_GainFocus_On_ToggleOpen = 1
nmap <F4> :TlistToggle<cr>

" lookupstring - find string in files in all subdirs {{{2
" First, cd to your top directory, use ":cd path/to/your/top/directory".
" Then, use <F6> to find string, default the word under cursor.
function! LookupString(str)
    let string = input("Lookup string: ", a:str)
    if string == ""
        return
    endif
    let filetype = input("Lookup File: ", "**/*")
    exe "vimgrep /" . string . "/ " . filetype
endfunction
nmap <f6> :call LookupString(expand("<cword>"))<cr>

"omnicppcomplete.vim - c,cpp member complete {{{2
let g:OmniCpp_DefaultNamespaces = ["std"]
let g:OmniCpp_MayCompleteScope = 1
let g:OmniCpp_SelectFirstItem = 0

" cscope - code browse {{{2
nmap <silent> <leader>ss :exe 'cs find s ' . expand("<cword>")<cr>
nmap <silent> <leader>sg :exe 'cs find g ' . expand("<cword>")<cr>
nmap <silent> <leader>sc :exe 'cs find c ' . expand("<cword>")<cr>
nmap <silent> <leader>st :exe 'cs find t ' . expand("<cword>")<cr>
nmap <silent> <leader>se :exe 'cs find e ' . expand("<cword>")<cr>
nmap <silent> <leader>sf :exe 'cs find f ' . expand("<cfile>")<cr>
nmap <silent> <leader>si :exe 'cs find i ' . expand("<cfile>")<cr>
nmap <silent> <leader>sd :exe 'cs find d ' . expand("<cword>")<cr>

" vimacs.vim - VIM emulate emacs actions in insert mode {{{2
" When use this plugin, you must use <Alt-n>, <Alt-p> to select the
" next, previous searched word in normal mode.
let g:VM_F10Menu = 0

" vimgdb - console gdb {{{2
" You must compile VIM with vimgdb
if has("gdb")
    set asm=0
    let g:vimgdb_debug_file = ""
    run macros/gdb_mappings.vim
endif

" man.vim - view man page in VIM {{{2
source $VIMRUNTIME/ftplugin/man.vim

" zoomwin.vim - zoomwin {{{2
nmap <leader>zz <C-w>o
